<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;

use App\Repositories\Eloquent\SmsRepository as Sms;

class SmsController extends Controller
{   
    protected $sms;

    public function __construct(Sms $sms)
    {
        parent::__construct();

        $this->sms = $sms;
    }


    public function sign()
    {
        $result = collect($this->sms->signed());

        $sign = $result->transform(function ($item, $key) {            
            if( strcmp($key,'SignStatus') === 0 )
            {
                switch ($item) {
                    case 0:
                        return $item = '审核中';
                        break;
                    case 1:                    
                        return $item = '审核通过';
                        break;
                    case 2:
                        return $item = '审核失败';
                        break;
                    default:
                        break;
                }
            }
            else{
                return $item;
            }
        });
        
        return view($this->view, compact('sign')); 
    }


    public function template()
    {
        $tpls = $this->sms->templated();
        
        return view($this->view, compact('tpls')); 
    }


    public function templateStore(Request $request)
    {
        return $this->sms->store($request);
    }


    public function templateCreate()
    {
        return view($this->view); 
    }

    public function templateRefresh(Request $request)
    {
        return $this->sms->refresh($request); 
    }


    public function show($id)
    {
        $tpl = $this->sms->show($id);

        if ($tpl instanceof JsonResponse && $tpl->getStatusCode() === 404) {                    
            return abort('404');
        }        

        return view($this->view, compact('tpl'));
    }


    public function edit($id)
    {
        $tpl = $this->sms->show($id);

        if ($tpl instanceof JsonResponse && $tpl->getStatusCode() === 404) {                    
            return abort('404');
        }        

        return view($this->view, compact('tpl'));
    }


    public function update(Request $request, $id)
    {
        return $this->sms->renew($request, $id);
    }
   
}
